<script lang="ts" setup>
import { ref } from 'vue'
import qs from 'qs'
import { GetData } from '@/api/modules/memberholiday'
import { GetAllData } from '@/api/modules/merchandiseControl'
import GetCacheData from '@/store/modules/NewPageCache'

const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()
const cacheData = GetCacheData()

const findFrom = ref({
  ...(cacheData.GetCacheData('memberholiday').fromData),
})
const dataList: any = reactive({
  list: [],
  productIdsOptions: [],
  productIdOptions: [],
})
const loading = ref(true)

async function GetDataList(PageIndex: number = 1, PageSize: number = 100) {
  const params = qs.stringify({
    PageIndex,
    PageSize,
    ...(findFrom.value.Name !== '' && ({ Name: findFrom.value.Name })),
    ...(findFrom.value.ProductIdsSec.length !== 0 && ({ ProductIdsSec: findFrom.value.ProductIdsSec })),
    ...(findFrom.value.ProductIdsNom.length !== 0 && ({ ProductIdsNom: findFrom.value.ProductIdsNom })),
  })
  await GetData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      loading.value = false
    }
  })
  loading.value = false
}
// 查询
function Inquire() {
  GetDataList()
  cacheData.SetCacheData('memberholiday', 1, 20, findFrom.value)
}

// 添加
function AddData() {
  router.push({
    name: 'memberholidayDetail',
  })
}

// 编辑
function EditData(id: number) {
  router.push({
    name: 'memberholidayDetail',
    query: {
      id,
    },
  })
}

onMounted(async () => {
  try {
    const params = qs.stringify({ ProductName: '', IsActive: true })
    const res1 = await GetAllData(params)
    dataList.productIdsOptions = res1.data

    const param = qs.stringify({ ProductName: '', IsActive: true, FlagList: [3] })
    const res2 = await GetAllData(param)
    dataList.productIdOptions = res2.data

    await GetDataList()
  }
  catch (error) {
    console.error('Error fetching data:', error)
  }
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="活动标题">
                <el-input v-model="findFrom.Name" style="width: 300px;" placeholder="请输入活动标题" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="秒杀商品">
                <el-select
                  v-model="findFrom.ProductIdsSec"
                  multiple
                  placeholder="请选择秒杀商品"
                  filterable
                >
                  <el-option
                    v-for="item in dataList.productIdOptions"
                    :key="item.productId"
                    :label="`${item.productId}--${item.productName}`"
                    :value="`${item.productId}`"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="会员专享商品">
                <el-select v-model="findFrom.ProductIdsNom" multiple placeholder="请选择会员专享商品" filterable>
                  <el-option
                    v-for="item in dataList.productIdsOptions"
                    :key="item.productId"
                    :label="`${item.productId}--${item.productName}`"
                    :value="`${item.productId}`"
                  />
                </el-select>
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button type="success" @click="AddData">
          添加
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="ID" width="80" />
        <el-table-column align="center" label="顶部banner" width="150">
          <template #default="scope">
            <ElImage style=" width: auto;height: 60px;" :src="imgDomain + scope.row.banner" />
          </template>
        </el-table-column>
        <el-table-column header-align="center" prop="name" label="标题" width="350" />
        <el-table-column align="center" prop="startDate" label="活动时间" width="350">
          <template #default="scope">
            {{ scope.row.startDate }} ~ {{ scope.row.endDate }}
          </template>
        </el-table-column>
        <el-table-column header-align="center" prop="productIdsSec" label="限时秒杀商品ID" width="350" />
        <el-table-column header-align="center" prop="productIdsNom" label="会员专享商品ID" width="350" />
        <el-table-column align="center" prop="addTime" label="添加时间" width="200" />
        <el-table-column align="center" prop="updateTime" label="更新时间" width="200" />
        <el-table-column align="center" label="操作" fixed="right" min-width="150">
          <template #default="scope">
            <el-button type="primary" @click="EditData(scope.row.id)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}
</style>
